package com.fengyunhe.common.captcha;

import org.patchca.color.ColorFactory;
import org.patchca.color.SingleColorFactory;
import org.patchca.filter.predefined.*;
import org.patchca.service.ConfigurableCaptchaService;
import org.patchca.utils.encoder.EncoderHelper;
import org.patchca.word.RandomWordFactory;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.*;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;

/**
 * 功能：验证码servlet
 * 作者： yangyan
 * 时间： 2014/8/29 .
 */
public class CaptchaServlet extends HttpServlet {

    private static final long serialVersionUID = 3796351198097771007L;
    public static String SESSION_KEY = "captcha";
    private static ConfigurableCaptchaService cs = null;
    private static ColorFactory cf = null;
    private static RandomWordFactory wf = null;
    private static Random r = new Random();
    private static CurvesRippleFilterFactory crff = null;
    private static MarbleRippleFilterFactory mrff = null;
    private static DoubleRippleFilterFactory drff = null;
    private static WobbleRippleFilterFactory wrff = null;
    private static DiffuseRippleFilterFactory dirff = null;


    @Override
    public void init() throws ServletException {
        super.init();
        if (this.getInitParameter("sessionKey") != null) {
            SESSION_KEY = this.getInitParameter("sessionKey");
        }
        cs = new ConfigurableCaptchaService();
        cf = new SingleColorFactory(new Color(25, 60, 170));
        wf = new RandomWordFactory();
        crff = new CurvesRippleFilterFactory(cs.getColorFactory());
        drff = new DoubleRippleFilterFactory();
        wrff = new WobbleRippleFilterFactory();
        dirff = new DiffuseRippleFilterFactory();
        mrff = new MarbleRippleFilterFactory();

        wf.setCharacters("23456789abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ");
        cs.setWordFactory(wf);
        cs.setColorFactory(cf);
        cs.setWidth(120);
        cs.setHeight(70);
    }

    @Override
    public void destroy() {
        wf = null;
        cf = null;
        cs = null;
        super.destroy();
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("image/png");
        response.setHeader("cache", "no-cache");
        wf.setMaxLength(5);
        wf.setMinLength(3);
        HttpSession session = request.getSession();
        OutputStream os = response.getOutputStream();
        switch (r.nextInt(5)) {
            case 0:
                cs.setFilterFactory(crff);
                break;
            case 1:
                cs.setFilterFactory(mrff);
                break;
            case 2:
                cs.setFilterFactory(drff);
                break;
            case 3:
                cs.setFilterFactory(wrff);
                break;
            case 4:
                cs.setFilterFactory(dirff);
                break;
        }
        String captcha = EncoderHelper.getChallangeAndWriteImage(cs, "png", os);
        session.setAttribute(SESSION_KEY, captcha);
        os.flush();
        os.close();
    }
}
